from log import Logger
import re,datetime
from misc.color_printer import ColorPrinter
logger = Logger(logname='log.txt', loglevel=1, logger="DYDM").getlog()
def parse_recv_msg(recv_msg):
    if "type@=" not in recv_msg:
        logger.debug(recv_msg)
    elif "type@=error" in recv_msg:
        logger.error("错误消息,可能认证失效")
    else:
        msg_content = recv_msg.replace("@S", "/").replace("@A=", ":").replace("@=", ":")
        # print(msg_content)
        try:
            msg_type = re.search('type:(.+?)\/', msg_content).group(1)

            if msg_type == "chatmsg":
                msg_type_zh = "弹幕消息"
                sender_id = re.search('\/uid:(.+?)\/', msg_content).group(1) if 'uid:' in msg_content else "unknown"
                nickname = re.search('\/nn:(.+?)\/', msg_content).group(1) if 'nn:' in msg_content else "unknown"
                content = re.search('\/txt:(.+?)\/', msg_content).group(1) if 'txt:' in msg_content else "unknown"
                level = re.search('\/level:(.+?)\/', msg_content).group(1) if 'level:' in msg_content else "unknown"
                client_type = re.search('\/ct:(.+?)\/', msg_content).group(
                    1) if 'ct:' in msg_content else "unknown"  # ct 默认值 0 web
                time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                print(
                    "|" + msg_type_zh + "| 【" + nickname + "】 <Lv:" + level + ">" +
                    "(" + sender_id + ")" + "[" + client_type + "]"  + "@ " + time + ": " + content + " ")

            elif msg_type == "uenter":
                msg_type_zh = "入房消息"
                user_id = re.search('\/uid:(.+?)\/', msg_content).group(1) if 'uid:' in msg_content else "unknown"
                nickname = re.search('\/nn:(.+?)\/', msg_content).group(1) if 'nn:' in msg_content else "unknown"
                strength = re.search('\/str:(.+?)\/', msg_content).group(1) if 'str:' in msg_content else "unknown"
                level = re.search('\/level:(.+?)\/', msg_content).group(1) if 'level:' in msg_content else "unknown"
                time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                print(
                    "|" + msg_type_zh + "| " + nickname +  "<Lv:" + level + ">" +  "(" + user_id + ")" + "[" + strength + "]"  + "@ " + time)

            elif msg_type == "dgb":
                msg_type_zh = "礼物赠送"
                level = re.search('\/level:(\d+?)\/', msg_content).group(
                    1) if 'level:' in msg_content else "unknown"
                user_id = re.search('\/uid:(.+?)\/', msg_content).group(1) if 'uid:' in msg_content else "unknown"
                nickname = re.search('\/nn:(.+?)\/', msg_content).group(1) if 'nn:' in msg_content else "unknown"
                strength = re.search('\/str:(.+?)\/', msg_content).group(1) if 'str:' in msg_content else "unknown"
                dw = re.search('\/dw:(.+?)\/', msg_content).group(1) if 'dw:' in msg_content else "unknown"
                gs = re.search('\/gs:(.+?)\/', msg_content).group(1) if 'gs:' in msg_content else "unknown"
                hits = re.search('\/hits:(.+?)\/', msg_content).group(1) if 'hits:' in msg_content else "unknown"
                time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                print(
                    "|" + msg_type_zh + "| " +  nickname +  "<Lv:" + level + ">" + "(" + user_id + ")"  +
                        "[" + dw + "]" +  "[" + gs + "]"  +
                        "[" + strength + "]" + "@ " + time + ": " + hits + " hits ")
        except Exception as e:
            print(e)
            logger.error("弹幕内容解析错误")


def align_left_str(self, raw_str, max_length, filled_chr):
    my_length = 0
    for i in range(0, len(raw_str)):
        if ord(raw_str[i]) > 127 or ord(raw_str[i]) <= 0:
            my_length += 1

        my_length += 1